---
- name: Install fish shell in Linux
  ansible.builtin.import_tasks: linux.yml
  when: ansible_distribution == "Ubuntu"

- name: Install fish shell in MacOS
  ansible.builtin.import_tasks: macos.yml
  when: ansible_distribution == "MacOSX"

- name: Link core config
  ansible.builtin.file:
    src: "{{ fish_src_dir }}/core"
    dest: "{{ fish_conf_dir }}/core"
    state: link

- name: Ensure custom dir
  ansible.builtin.file:
    path: "{{ fish_conf_custom_dir }}"
    state: directory

- name: Check custom config stat
  register: custom
  ansible.builtin.stat:
    path: "{{ fish_conf_custom_dir }}/init.fish"

- name: Create custom config
  when: custom.stat.exists is defined and not custom.stat.exists
  ansible.builtin.copy:
    src: "{{ fish_src_dir }}/custom.fish"
    dest: "{{ fish_conf_custom_dir }}/init.fish"

- name: Link config
  ansible.builtin.file:
    state: link
    src: "{{ fish_src_dir }}/config.fish"
    dest: "{{ fish_conf_dir }}/config.fish"
    force: true

- name: List fish plugins
  ansible.builtin.command: cat {{ fish_conf_dir }}/fish_plugins
  register: plugins
  changed_when: false

- name: Install fish plugins
  ansible.builtin.shell:
    executable: fish
    cmd: fisher install {{ item }}
  register: installed_plugins
  changed_when: "'Installed' in installed_plugins.stdout"
  when: item not in plugins
  loop: "{{ fish_plugins }}"
